﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Utility.DBMS;
using System.Utility.Messaging;
using System.Utility.UI;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class ReviewOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if(!IsPostBack)
            {
                GridView1.Visible = false;
                PopulateStates();
            }
        }

        private void PopulateStates()
        {
            ddState.DataSource = DBHelper.GetDataset("GetStates");
            ddState.DataTextField = "State";
            ddState.DataValueField = "State";

            ddState.DataBind();
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                LoadGrid();
                GridView1.Visible = true;
            }
            catch (Exception ex)
            {
                MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
            }
            PageUtility.RefreshByType(new Control[] { ddState, CalenderUserControl1, CalenderUserControl,});
        }

        private void LoadGrid()
        {
            if (((TextBox)CalenderUserControl.FindControl("TextBox1")).Text == string.Empty)
            {
                ((TextBox)CalenderUserControl.FindControl("TextBox1")).Text = DateTime.Now.ToString();
            }
            if (((TextBox)CalenderUserControl1.FindControl("TextBox1")).Text == string.Empty)
            {
                ((TextBox)CalenderUserControl1.FindControl("TextBox1")).Text = DateTime.Now.ToString();
            }
            if (txtOrderId.Value == string.Empty && txtOrderBy.Value == string.Empty)
            {
                GridView1.DataSource = DBHelper.GetDataset("GetOrdersByParameters", new object[] { ddState.Items[ddState.SelectedIndex].Value, Convert.ToDateTime(((TextBox)CalenderUserControl1.FindControl("TextBox1")).Text), Convert.ToDateTime(((TextBox)CalenderUserControl.FindControl("TextBox1")).Text),null,null });
            }
            else if (txtOrderId.Value == string.Empty)
            {
                GridView1.DataSource = DBHelper.GetDataset("GetOrdersByParameters", new object[] { ddState.Items[ddState.SelectedIndex].Value, Convert.ToDateTime(((TextBox)CalenderUserControl1.FindControl("TextBox1")).Text), Convert.ToDateTime(((TextBox)CalenderUserControl.FindControl("TextBox1")).Text), null,txtOrderBy.Value });
            }
            else if (txtOrderBy.Value == string.Empty)
            {
                GridView1.DataSource = DBHelper.GetDataset("GetOrdersByParameters", new object[] { ddState.Items[ddState.SelectedIndex].Value, Convert.ToDateTime(((TextBox)CalenderUserControl1.FindControl("TextBox1")).Text), Convert.ToDateTime(((TextBox)CalenderUserControl.FindControl("TextBox1")).Text), txtOrderId.Value,null });
            }
            else
            {
                GridView1.DataSource = DBHelper.GetDataset("GetOrdersByParameters", new object[] { ddState.Items[ddState.SelectedIndex].Value, Convert.ToDateTime(((TextBox)CalenderUserControl1.FindControl("TextBox1")).Text), Convert.ToDateTime(((TextBox)CalenderUserControl.FindControl("TextBox1")).Text), txtOrderId.Value, txtOrderBy.Value });
            }
            GridView1.DataBind();
       }

        protected void grdData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadGrid();
        }

       


    }
}